package site.marqstree.latte.ec.main.sort.content;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.entity.node.BaseNode;
import java.util.ArrayList;
import java.util.List;
/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.sort.content
 * 文件名: SectionDataConverter
 * 创建者: marqstree
 * 创建时间: 2020/1/30 9:47
 * 描述: TODO
 */
public class SectionDataConverter {

    final List<BaseNode> convert(String json) {
        final JSONArray dataArray = JSON.parseObject(json).getJSONArray("data");
        List<BaseNode> rootList = new ArrayList<>();

        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);
            final int id = data.getInteger("id");
            final String title = data.getString("section");

            final JSONArray goods = data.getJSONArray("goods");
            //商品内容循环
            final int goodSize = goods.size();
            List<BaseNode> itemList = new ArrayList<>();
            for (int j = 0; j < goodSize; j++) {
                final JSONObject contentItem = goods.getJSONObject(j);
                final int goodsId = contentItem.getInteger("goods_id");
                final String goodsName = contentItem.getString("goods_name");
                final String goodsThumb = contentItem.getString("goods_thumb");
                //获取内容
                final ItemNode itemEntity = new ItemNode(goodsId,goodsName,goodsThumb);
                //添加内容
                itemList.add(itemEntity);
            }
            //商品内容循环结束

            //添加title
            RootNode rootNode = new RootNode(itemList,id,title);
            rootList.add(rootNode);
        }
        //Section循环结束

        return rootList;
    }
}
